<template>
  <div id="bookOrder">
    <com-head :opacity="0">我的书籍</com-head>
    <div class="nav">
      <div
        v-for="(item,index) in orderlist"
        :key="index"
        :class="{active: status==index}"
        @click="listSelect(index)"
      >{{item}}</div>
    </div>
    <div class="order" v-for="(item,index) in message" :key="item.order_id">
      <div class="orderTop">
        <div class="box1">下单时间：{{item.order_create_time}}</div>
        <div class="box2">{{server[item.status]}}</div>
      </div>
      <div class="orderCenter">
        <div class="box1">
          <img :src="item.image">
        </div>
        <div class="box2">
          <p>
            {{item.book}}
            <i class="iconfont icon-xiayi"/>
          </p>
          <p>{{item.book_sign}}</p>
        </div>
        <div class="box3">
          <p>&yen;{{item.order_pay_price}}</p>
          <p>x{{item.number}}</p>
        </div>
      </div>
      <div class="orderFoot">
        <div class="sumprice">共{{item.number}}件商品 合计：¥{{ item.number * item.order_pay_price}}</div>
        <button class="noborder" v-if="item.status=='1'">已下单，等待商家发货</button>
        <button v-if="item.status=='2'" @click="lookwuliu(index)">查看物流</button>
        <button
          class="evaluate"
          v-if="item.status=='2'"
          @click="surereceive(item.order_id,index)"
        >确定收货</button>
        <button v-if="item.status=='3'" @click="delorder(item.order_id,index)">删除订单</button>
      </div>
    </div>
    <div v-if="!message.length" style="margin-top: 30vh; text-align: center;">暂无订单</div>
  </div>
</template>

<script>
export default {
  name: "bookOrder",
  data() {
    return {
      status: 0,
      message: [],
      server: [
        "",
        "待发货",
        "待收货",
        "交易已完成",
        "删除订单",
        "确认收货",
        "查看物流"
      ],
      orderlist: ["全部", "待发货", "待收货", "已完成"]
    };
  },

  computed: {},

  created() {},

  mounted() {
    this.loading("0");
  },

  methods: {
    loading(type) {
      this.axios
        .post("user/order", {
          token: this.token(),
          status: type
        })
        .then(({ data }) => {
          console.log(data);
          if (data.code === "200") {
            this.message = data.data;
          } else if (data.code === "201") {
            this.$bus.$emit("toast", data.msg);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    // 删除订单
    delorder(id, index) {
       this.$bus.$emit("comAlert", {
          title: "确定删除",
          button: [
            {
              text: "确认",
              callback: () => {
                this.axios
                  .post("user/orderout", {
                    order_id: id
                  })
                  .then(({ data }) => {
                    console.log(data);
                    if (data.code === "200") {
                      this.$bus.$emit("toast", data.msg);
                      this.message.splice(index, 1);
                    } else if (data.code === "201") {
                      this.$bus.$emit("toast", data.msg);
                    }
                  })
                  .catch(function(error) {
                    console.log(error);
                  });
              }
            },
            {
              text: "取消",
              callback: () => {}
            }
          ]
        });     
    },
    // 确定收货
    surereceive(id, index) {
      this.$bus.$emit("comAlert", {
          title: "确定删除",
          button: [
            {
              text: "确认",
              callback: () => {
                this.axios
                  .post("user/overorder", {
                    order_id: id
                  })
                  .then(({ data }) => {
                    console.log(data);
                    if (data.code === "200") {
                      this.$bus.$emit("toast", data.msg);
                      this.message.splice(index, 1);
                    } else if (data.code === "201") {
                      this.$bus.$emit("toast", data.msg);
                    }
                  })
                  .catch(function(error) {
                    console.log(error);
                  });
              }
            },
            {
              text: "取消",
              callback: () => {}
            }
          ]
        });     
    },
    // 查看物流
    lookwuliu(index) {
      console.log(this.message[index].tracking_number);
      this.$bus.$emit("comAlert", {
        title: "物流单号",
        info: this.message[index].tracking_number,
        button: [
          {
            text: "确认",
            callback: () => {}
          }
        ]
      });
    },
    listSelect(index) {
      this.message = [];
      this.status = index;
      this.loading(index);
    }
  }
};
</script>
<style lang='scss' scoped>
#bookOrder {
  padding-top: 164px;
  .nav {
    position: fixed;
    top: 88px;
    width: 750px;
    height: 76px;
    background: #f8f8f8;
    display: flex;
    justify-content: space-around;
    align-items: center;
    z-index: 100;
    div {
      width: 25%;
      height: 76px;
      font-size: 26px;
      text-align: center;
      line-height: 75px;
    }
    .active {
      color: #41d7bd;
    }
  }
  .order {
    width: 690px;
    margin: 10px auto;
    background: rgba(255, 255, 255, 1);
    border-radius: 20px;
    .orderTop {
      margin: 0 32px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 24px;
      color: #666;
      line-height: 66px;
      .box2 {
        color: #ff7a13;
      }
    }
    .orderCenter {
      margin: 20px 30px;
      display: flex;
      justify-content: space-between;
      //   align-items: center;
      .box1 {
        width: 120px;
        height: 156px;
        border-radius: 6px;
        overflow: hidden;
        img {
          width: 100%;
        }
      }
      .box2 {
        width: 300px;
        margin-left: 30px;
        p:nth-of-type(1) {
          margin-top: 10px;
          font-size: 30px;
          line-height: 40px;
          color: #222;
        }
        p:nth-of-type(2) {
          font-size: 24px;
          color: #666;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;
        }
      }
      .box3 {
        width: 160px;
        text-align: right;
        color: #666;
        font-size: 28px;
        p:nth-of-type(1) {
          margin-top: 10px;
          color: #222;
          font-size: 24px;
        }
      }
    }
    .orderFoot {
      text-align: right;
      margin: 0px 30px;
      padding-bottom: 20px;
      box-sizing: border-box;
      font-size: 26px;
      .sumprice {
        line-height: 70px;
        margin: 10px auto;
        border-bottom: 1Px solid #f6f6f6;
        text-align: right;
        color: rgba(34, 34, 34, 1);
        font-size: 26px;
      }
      button {
        margin: 20px 0 10px 20px;
        background: none;
        padding: 10px 20px;
        font-size: 26px;
        border-radius: 24px;
        color: #999;
        border: 1Px solid #999;
      }
      .evaluate {
        color: #ff7a13;
        border: 1Px solid #ff7a13;
      }
      .noborder {
        color: #999;
        border: none;
      }
    }
  }
}
</style>